package com.iflytek.edu.db.ext.pool;

import com.alibaba.druid.DbType;
import com.alibaba.druid.filter.FilterChain;
import com.alibaba.druid.filter.FilterEventAdapter;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
import com.iflytek.edu.db.core.parse.SqlAdapter;
import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;

/**
 * Druid适配器
 *
 * @author linkai
 * @date 2022/2/11
 */
@Slf4j
public class DruidFilterAdapter extends FilterEventAdapter {
    private final SqlAdapter sqlParse;

    public DruidFilterAdapter() {
        this.sqlParse = new SqlAdapter(DbType.dm);
    }

    @Override
    public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection, String sql) throws SQLException {
        log.info("druid adapter before! \nsql = {}", sql);
        sql = sqlParse.parse(sql);
        log.info("druid adapter after! \nsql = {}", sql);
        return super.connection_prepareStatement(chain, connection, sql);
    }
}
